/// <remarks>
/// FilterExtensions Class
/// Authors: Louis-Philippe Lebouthillier.
/// </remarks>

using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;
using System.Xml;
using System.Xml.Serialization;
using VcLib;

namespace VcGUI
{
    /// <summary>
    /// url: http://www.c-sharpcorner.com/UploadFile/bipinjoshi/serializingObjectsinCS11102005234746PM/serializingObjectsinCS.aspx
    /// </summary>
    public partial class FilterExtensions : Form
    {
        private Logger logger = Logger.Instance;
        private string projectPath;
        AddFilesFrm addFilesFrm;

        public FilterExtensions(string projectPath, AddFilesFrm addFilesFrm)
        {
            InitializeComponent();
            this.projectPath = projectPath;
            this.addFilesFrm = addFilesFrm;

            this.AcceptButton = this.btnAdd;
            this.CancelButton = this.btnCancel;

            try
            {
                PopulateListExtension(projectPath);
            }
            catch
            {
                this.logger.Info("No extension excluded");
            }
        }

        private void PopulateListExtension(string projectPath)
        {
            try
            {
                this.logger.DebugInfo("Start populate list of excluded extension");
                VcLib.ExtensionsManager extensionManager = new VcLib.ExtensionsManager();
                this.chklstExcludedExtensions.Items.Clear();
                this.chklstExcludedExtensions.BeginUpdate();

                IDictionary<string, bool> dics = extensionManager.LoadFromXML(projectPath);
                foreach (KeyValuePair<string, bool> dic in dics)
                {
                    chklstExcludedExtensions.Items.Add(dic.Key, dic.Value);
                }
            }
            catch (Exception ex)
            {
                this.logger.Log(ex.Message);
            }
        }

        //Simply verify if the extension is already present in the list
        private bool UniqueExtension()
        {
            int nb = this.chklstExcludedExtensions.Items.Count;
            string extension = this.txtExtension.Text;
            for (int i = 0; i < nb; i++)
            {
                if (extension == this.chklstExcludedExtensions.Items[i].ToString())
                {
                    this.logger.Error("Extension already present in the list");
                    return false;
                }
            }
            return true;
        }

        private void btAdd_Click(object sender, EventArgs e)
        {
            AddExtension();
        }

        private void txtExtension_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                AddExtension();
        }

        private void AddExtension()
        {
            //Extension have to begin by a dot Ex:".exe" and to be unique to be added in the list
            if (UniqueExtension() && txtExtension.Text.StartsWith("."))
            {
                string extLower = txtExtension.Text.ToLower();
                this.chklstExcludedExtensions.Items.Add(extLower, true);
                this.logger.DebugInfo("Successfully add extesion " + extLower + " to the filtered extension list");
                this.txtExtension.Clear();
            }
            else
            {
                this.logger.Error("Bad entry, try again");
            }
            this.txtExtension.Focus();
        }

        private void btClose_Click(object sender, EventArgs e)
        {
            logger.DebugInfo("Excluded extension set not saved");
            this.Close();
        }

        private void btRemove_Click(object sender, EventArgs e)
        {
            if (this.chklstExcludedExtensions.SelectedItem != null)
            {
                this.logger.DebugInfo("Extension " + this.chklstExcludedExtensions.SelectedItem.ToString() + " have been remove");
                this.chklstExcludedExtensions.Items.Remove(this.chklstExcludedExtensions.SelectedItem);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            IDictionary<string, bool> dics = new Dictionary<string, bool>();
            VcLib.ExtensionsManager extensionManager = new VcLib.ExtensionsManager();
            int nb = this.chklstExcludedExtensions.Items.Count;

            //fill the dictionary of extension with state
            for (int i = 0; i < nb; i++)
            {
                dics.Add(this.chklstExcludedExtensions.Items[i].ToString(), this.chklstExcludedExtensions.GetItemChecked(i));
            }

            extensionManager.SaveToXML(dics, this.projectPath);
            this.logger.DebugInfo("Sucessfully save the excluded extension list");
            this.addFilesFrm.RefreshList();
            this.Close();
        }
    }
}
